﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.UserManage.rule
{
    /// <summary>
    /// 规则列表
    /// </summary>
    public partial class userrulegrid : UserPage
    {
        protected string parmStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = "";

            if (ViewState["condition"] == null || ViewState["condition"].ToString() == "")
            {
                if (parmStr != "") strWhere = parmStr;
            }
            else
            {
                if (parmStr != "") strWhere = ViewState["condition"].ToString() + " AND " + parmStr;
                else strWhere = ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = Rules.GetRulesCount(strWhere);
            DataGrid1.DataSource = Rules.GetRulesByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "rtype asc,rid asc,id", "desc");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            ViewState["condition"] = Rules.GetUserRuleSearchCondition(Utils.StrToInt(ruletype.SelectedValue, 0), SASRequest.GetString("ajaxcarrierlist_value"), SASRequest.GetString("ajaxroutelinedatalist_value"), destport.Text, SASRequest.GetString("username"));
            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        protected void del_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Rules.DeleteRulesByIds(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            string headname = "用户规则列表";
            headname = "用户规则列表";
            parmStr = "((oid = " + companyinfo.id + " AND rtype = 0 AND uid = -2 AND rid = 0) OR (oid = 0 AND cid = " + companyinfo.id + " AND rtype <> 3))";
            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = headname;
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            del.Click += new EventHandler(del_Click);

            if (!OpAction(OprateAction.Delete)) del.Visible = false;
            if (!OpAction(OprateAction.Create)) add.Visible = false;

        }

        #endregion
    }
}